home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility2 / resgauge.zip / RESGAUGE.C < prev    next >
C/C++ Source or Header  |  1992-08-23  |  14KB  |  461 lines

  1. // ============================================================================
  2. // resgauge.c -- a gauge of free system resources.
  3. //
  4. // Richard Franklin Albury
  5. // PO Box 19652
  6. // Raleigh, NC 27619-9652
  7. //
  8. // CIS: 76477,534
  9. // ============================================================================
  10.  
  11.  
  12. #define MAIN // make globals local to this module
  13.  
  14.  
  15. #include <windows.h>
  16. #include <toolhelp.h>
  17. #include "resgauge.h"
  18.  
  19.  
  20. // ---------------------
  21. // default configuration
  22. // ---------------------
  23. CONFIG GcfgDef =
  24.        {
  25.            FALSE,              // TRUE if the alarm is enabled
  26.            FALSE,              // TRUE if "Keep on top" is selected
  27.            ALARM_BEEP,         // beep or flash
  28.            MONITOR_BOTH,       // GDI, User, or both
  29.            10,                 // percent at which alarm sounds
  30.            RGB(255,   0,   0), // gauge color for GDI
  31.            RGB(  0, 255,   0), // gauge color for User
  32.            RGB(  0,   0, 255)  // gauge color for both
  33.        };
  34.  
  35. // -------------------------------------
  36. // lookup table for plotting the "gauge"
  37. // -------------------------------------
  38. OFFSET GaOffset[] =
  39.        {
  40.            {  0,  91}, {  0,  94}, {  0,  97}, {  0, 100}, {  1,  84},
  41.            {  1,  87}, {  2,  78}, {  2,  81}, {  3,  75}, {  4,  72},
  42.            {  5,  69}, {  6,  66}, {  7,  63}, {  8,  60}, { 10,  57},
  43.            { 11,  55}, { 12,  52}, { 14,  49}, { 16,  46}, { 17,  44},
  44.            { 19,  41}, { 21,  39}, { 23,  36}, { 25,  34}, { 27,  32},
  45.            { 29,  29}, { 32,  27}, { 34,  25}, { 36,  23}, { 39,  21},
  46.            { 41,  19}, { 44,  17}, { 46,  16}, { 49,  14}, { 52,  12},
  47.            { 55,  11}, { 57,  10}, { 60,   8}, { 63,   7}, { 66,   6},
  48.            { 69,   5}, { 72,   4}, { 75,   3}, { 78,   2}, { 81,   2},
  49.            { 84,   1}, { 87,   1}, { 91,   0}, { 94,   0}, { 97,   0},
  50.            {100,   0}, {103,   0}, {106,   0}, {109,   0}, {113,   1},
  51.            {116,   1}, {119,   2}, {122,   2}, {125,   3}, {128,   4},
  52.            {131,   5}, {134,   6}, {137,   7}, {140,   8}, {143,  10},
  53.            {145,  11}, {148,  12}, {151,  14}, {154,  16}, {156,  17},
  54.            {159,  19}, {161,  21}, {164,  23}, {166,  25}, {168,  27},
  55.            {171,  29}, {173,  32}, {175,  34}, {177,  36}, {179,  39},
  56.            {181,  41}, {183,  44}, {184,  46}, {186,  49}, {188,  52},
  57.            {189,  55}, {190,  57}, {192,  60}, {193,  63}, {194,  66},
  58.            {195,  69}, {196,  72}, {197,  75}, {198,  78}, {198,  81},
  59.            {199,  84}, {199,  87}, {200,  91}, {200,  94}, {200,  97},
  60.            {200, 100}
  61.        };
  62.  
  63.  
  64. // ============================================================================
  65. // > > > > > > > > > > > > > > >  code  begins  < < < < < < < < < < < < < < < <
  66. // ============================================================================
  67. // The "main()" for Windows: calls the initialization functions and enters the
  68. // message processing loop.  Returns the wParam element of the MSG when a
  69. // WM_QUIT message has been processed by the window function.
  70. // ----------------------------------------------------------------------------
  71. int
  72. PASCAL
  73. WinMain(
  74. HINSTANCE hInstance,     // current instance
  75. HINSTANCE hPrevInstance, // previous instance
  76. LPSTR     lpstrCmdLine,  // command line
  77. int       nCmdShow)      // window state (usually open or iconic)
  78. {
  79.     int   nReturn;
  80.     MSG   msg;
  81.  
  82.     // assume failure and save the instance handle
  83.     nReturn = 1;
  84.     GhInst = hInstance;
  85.  
  86.     // get the configuration info and process the command line
  87.     GetConfig();
  88.     ProcessCmdLine(lpstrCmdLine);
  89.  
  90.     // call the initialization functions and enter the message loop
  91.     if ( InitApplication(hPrevInstance) && InitInstance() )
  92.     {
  93.         while ( GetMessage(&msg, NULL, 0, 0) )
  94.         {
  95.             TranslateMessage(&msg);
  96.             DispatchMessage(&msg);
  97.         }
  98.         nReturn = msg.wParam;
  99.     }
  100.  
  101.     return(nReturn);
  102. } // WinMain
  103.  
  104.  
  105. // ----------------------------------------------------------------------------
  106. // Processes messages for the main window.
  107. // ----------------------------------------------------------------------------
  108. LRESULT
  109. WINAPI
  110. MainWndProc(
  111. HWND        hWnd,     // window handle
  112. UINT        wMessage, // type of message
  113. WPARAM      wParam,   // 16 bits of information
  114. LPARAM      lParam)   // 32 additional bits of information
  115. {
  116.     HMENU   hMenu;
  117.     LRESULT lrReturn;
  118.     WORD    wTmp;
  119.  
  120.     lrReturn = 0L;
  121.     switch ( wMessage )
  122.     {
  123.         case WM_CREATE:
  124.             MainCreate(hWnd);
  125.             break;
  126.  
  127.         // disable Restore, Size, Minimize, and Maximize on the system menu
  128.         case WM_INITMENU:
  129.             hMenu = (HMENU) wParam;
  130.             EnableMenuItem(hMenu, SC_RESTORE,  MF_BYCOMMAND | MF_GRAYED);
  131.             EnableMenuItem(hMenu, SC_SIZE,     MF_BYCOMMAND | MF_GRAYED);
  132.             EnableMenuItem(hMenu, SC_MINIMIZE, MF_BYCOMMAND | MF_GRAYED);
  133.             EnableMenuItem(hMenu, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
  134.             DrawMenuBar(hWnd);
  135.             break;
  136.  
  137.         case WM_TIMER:
  138.             MainTimer(hWnd);
  139.             break;
  140.  
  141.         case WM_SYSCOMMAND:
  142.             switch ( wParam )
  143.             {
  144.                 case SC_KEEP_ON_TOP:
  145.                     hMenu = GetSystemMenu(hWnd, FALSE);
  146.                     wTmp = GetMenuState(hMenu, SC_KEEP_ON_TOP, MF_BYCOMMAND);
  147.                     HandleKeepOnTop(hWnd,
  148.                                     hMenu,
  149.                                     ((wTmp & MF_CHECKED) != MF_CHECKED));
  150.                     SaveKeepOnTop();
  151.                     break;
  152.  
  153.                 case SC_CONFIGURE:
  154.                     DialogBox(GhInst,
  155.                               MAKEINTRESOURCE(IDDB_CONFIGURE),
  156.                               hWnd,
  157.                               ConfigDlgProc);
  158.                     break;
  159.  
  160.                 case SC_ABOUT:
  161.                     DialogBox(GhInst,
  162.                               MAKEINTRESOURCE(IDDB_ABOUT),
  163.                               hWnd,
  164.                               AboutDlgProc);
  165.                     break;
  166.  
  167.                 default:
  168.                     lrReturn = DefWindowProc(hWnd, wMessage, wParam, lParam);
  169.                     break;
  170.             }
  171.             break;
  172.  
  173.         case WM_PAINT:
  174.             MainPaint(hWnd);
  175.             break;
  176.  
  177.         case WM_QUERYOPEN:
  178.             break;
  179.  
  180.         case WM_DESTROY:
  181.             KillTimer(hWnd, TIMER_ID);
  182.             PostQuitMessage(0);
  183.             break;
  184.  
  185.         default:
  186.             lrReturn = DefWindowProc(hWnd, wMessage, wParam, lParam);
  187.             break;
  188.     }
  189.  
  190.     return(lrReturn);
  191. } // MainWndProc
  192.  
  193.  
  194. // ----------------------------------------------------------------------------
  195. // Handles the WM_CREATE message for the main window.
  196. // ----------------------------------------------------------------------------
  197. void
  198. FAR
  199. PASCAL
  200. MainCreate(
  201. HWND      hWnd) // window handle
  202. {
  203.     char  szMenuStr[32];
  204.     HMENU hMenu;
  205.  
  206.     GwFree = GetSystemResources();
  207.     FormatLabel();
  208.     hMenu = GetSystemMenu(hWnd, FALSE);
  209.     AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
  210.     LoadString(GhInst, IDSM_KEEP_ON_TOP, szMenuStr, sizeof(szMenuStr) - 1);
  211.     AppendMenu(hMenu, MF_STRING, SC_KEEP_ON_TOP, szMenuStr);
  212.     LoadString(GhInst, IDSM_CONFIGURE, szMenuStr, sizeof(szMenuStr) - 1);
  213.     AppendMenu(hMenu, MF_STRING, SC_CONFIGURE, szMenuStr);
  214.     LoadString(GhInst, IDSM_ABOUT, szMenuStr, sizeof(szMenuStr) - 1);
  215.     AppendMenu(hMenu, MF_STRING, SC_ABOUT, szMenuStr);
  216.     HandleKeepOnTop(hWnd, hMenu, GcfgCur.fKeepOnTop);
  217.     DrawMenuBar(hWnd);
  218. } // MainCreate
  219.  
  220.  
  221. // ----------------------------------------------------------------------------
  222. // Handles the WM_PAINT message for the main window.
  223. // ----------------------------------------------------------------------------
  224. void
  225. FAR
  226. PASCAL
  227. MainPaint(
  228. HWND            hWnd) // window handle
  229. {
  230.     int         xText;
  231.     int         yText;
  232.     int         wX;
  233.     int         wY;
  234.     DWORD       dwExtent;
  235.     HBRUSH      hBrush;
  236.     HBRUSH      hOldBrush;
  237.     HDC         hDC;
  238.     PAINTSTRUCT ps;
  239.     RECT        rect;
  240.  
  241.     // begin painting
  242.     hDC = BeginPaint(hWnd, &ps);
  243.  
  244.     // get the dimensions of the label string
  245.     SelectObject(hDC, GetStockObject(ANSI_VAR_FONT));
  246.     dwExtent = GetTextExtent(hDC, GszBuf, lstrlen(GszBuf));
  247.  
  248.     // get the dimensions of the the window and frame the window
  249.     GetClientRect(hWnd, &rect);
  250.     FrameRect(hDC, &rect, GetStockObject(BLACK_BRUSH));
  251.  
  252.     // calculate where the label goes
  253.     xText = rect.left +
  254.             ((rect.right - rect.left) - LOWORD(dwExtent)) / 2;
  255.     yText = rect.top + rect.bottom - HIWORD(dwExtent) - 1;
  256.     yText = rect.bottom - HIWORD(dwExtent) - 1;
  257.  
  258.     // modify the rectangle to hold the gauge pie
  259.     rect.bottom = yText;
  260.     InflateRect(&rect, -2, -2);
  261.  
  262.     // calculate the origin of the pie
  263.     wX = rect.right - rect.left;
  264.     wY = rect.bottom - rect.top;
  265.  
  266.     // create a brush for the gauge pie and save the old brush
  267.     if ( GcfgCur.wMonitor == MONITOR_GDI )
  268.         hBrush = CreateSolidBrush(GcfgCur.dwColorGDI);
  269.     else if ( GcfgCur.wMonitor == MONITOR_USER )
  270.         hBrush = CreateSolidBrush(GcfgCur.dwColorUser);
  271.     else
  272.         hBrush = CreateSolidBrush(GcfgCur.dwColorBoth);
  273.     hOldBrush = SelectObject(hDC, hBrush);
  274.  
  275.     // draw the gauge pie
  276.     Pie(hDC,
  277.         rect.left,
  278.         rect.top,
  279.         rect.right,
  280.         rect.bottom + (rect.bottom - rect.top) + 1,
  281.         rect.left + (GaOffset[GwFree].wX * wX) / 200,
  282.         rect.top  + (GaOffset[GwFree].wY * wY) / 100,
  283.         rect.left,
  284.         rect.bottom);
  285.  
  286.     // restore the old brush and delete the new brush
  287.     SelectObject(hDC, hOldBrush);
  288.     DeleteObject(hBrush);
  289.  
  290.     // draw the label
  291.     TextOut(hDC, xText, yText, GszBuf, lstrlen(GszBuf));
  292.  
  293.     // stop painting
  294.     EndPaint(hWnd, &ps);
  295. } // MainPaint
  296.  
  297.  
  298. // ----------------------------------------------------------------------------
  299. // Handles the WM_TIMER message for the main window.
  300. // ----------------------------------------------------------------------------
  301. void
  302. FAR
  303. PASCAL
  304. MainTimer(
  305. HWND     hWnd) // window handle
  306. {
  307.     WORD wFree;
  308.  
  309.     wFree = GetSystemResources();
  310.     if ( (GcfgCur.fAlarmEnabled) && (wFree <= GcfgCur.wThreshold) )
  311.     {
  312.         if ( GcfgCur.wAlarmType == ALARM_FLASH )
  313.         {
  314.             SetWindowPos(hWnd,
  315.                          HWND_TOP,
  316.                          0, 0, 0, 0,
  317.                          SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
  318.             FlashWindow(hWnd, TRUE);
  319.         }
  320.         else
  321.         {
  322.             MessageBeep(0);
  323.         }
  324.     }
  325.     if ( wFree != GwFree )
  326.     {
  327.         // save the new value
  328.         GwFree = wFree;
  329.  
  330.         // format the gauge window label
  331.         FormatLabel();
  332.  
  333.         // force a repaint
  334.         InvalidateRect(hWnd, NULL, TRUE);
  335.     }
  336. } // MainTimer
  337.  
  338.  
  339. // ----------------------------------------------------------------------------
  340. // Handles the toggling of the "Keep on top" system menu option.
  341. // ----------------------------------------------------------------------------
  342. void
  343. FAR
  344. PASCAL
  345. HandleKeepOnTop(
  346. HWND     hWnd,       // window handle
  347. HMENU    hMenu,      // system menu handle
  348. BOOL     fKeepOnTop) // TRUE if "Keep on top" was selected
  349. {
  350.     HWND hWndInsertAfter;
  351.     WORD wCheck;
  352.  
  353.     if ( fKeepOnTop )
  354.     {
  355.         wCheck = MF_BYCOMMAND | MF_CHECKED;
  356.         hWndInsertAfter = HWND_TOPMOST;
  357.         GcfgCur.fKeepOnTop = TRUE;
  358.     }
  359.     else
  360.     {
  361.         wCheck = MF_BYCOMMAND | MF_UNCHECKED;
  362.         hWndInsertAfter = HWND_NOTOPMOST;
  363.         GcfgCur.fKeepOnTop = FALSE;
  364.     }
  365.     CheckMenuItem(hMenu, SC_KEEP_ON_TOP, wCheck);
  366.     SetWindowPos(hWnd,
  367.                  hWndInsertAfter,
  368.                  0, 0, 0, 0,
  369.                  SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
  370. } // HandleKeepOnTop
  371.  
  372.  
  373. // ----------------------------------------------------------------------------
  374. // Centers a dialog.
  375. // ----------------------------------------------------------------------------
  376. void
  377. FAR
  378. PASCAL
  379. CenterDialog(
  380. HWND      hDlg)
  381. {
  382.     short x;
  383.     short y;
  384.     short xSize;
  385.     short ySize;
  386.     RECT  rect;
  387.  
  388.     GetWindowRect(hDlg, &rect);
  389.     xSize = rect.right - rect.left;
  390.     ySize = rect.bottom - rect.top;
  391.     x = GetSystemMetrics(SM_CXSCREEN);
  392.     y = GetSystemMetrics(SM_CYSCREEN);
  393.     MoveWindow(hDlg, (x - xSize) / 2, (y - ySize) / 2, xSize, ySize, TRUE);
  394. } // CenterDialog
  395.  
  396.  
  397. // ----------------------------------------------------------------------------
  398. // Uses the SystemHeapInfo() call and the algorithm given in TIPS.TXT to
  399. // calculate the percentage of free system resources.
  400. // ----------------------------------------------------------------------------
  401. WORD
  402. FAR
  403. PASCAL
  404. GetSystemResources(void)
  405. {
  406.     SYSHEAPINFO shi;
  407.     WORD        wReturn;
  408.  
  409.     wReturn = 0;
  410.     shi.dwSize = sizeof(SYSHEAPINFO);
  411.     if ( SystemHeapInfo(&shi) )
  412.     {
  413.         switch ( GcfgCur.wMonitor )
  414.         {
  415.             case MONITOR_GDI:
  416.                 wReturn = shi.wGDIFreePercent;
  417.                 break;
  418.  
  419.             case MONITOR_USER:
  420.                 wReturn = shi.wUserFreePercent;
  421.                 break;
  422.  
  423.             case MONITOR_BOTH:
  424.                 wReturn = min(shi.wGDIFreePercent, shi.wUserFreePercent);
  425.                 break;
  426.         }
  427.     }
  428.  
  429.     return(wReturn);
  430. } // GetSystemResources
  431.  
  432.  
  433. // ----------------------------------------------------------------------------
  434. // Builds the label string for the gauge window.
  435. // ----------------------------------------------------------------------------
  436. void
  437. FAR
  438. PASCAL
  439. FormatLabel(void)
  440. {
  441.     char szFormat[32];
  442.     WORD wTmp;
  443.  
  444.     // determine the monitor mode
  445.     if ( GcfgCur.wMonitor == MONITOR_GDI )
  446.         wTmp = IDS_FORMAT_GDI;
  447.     else if ( GcfgCur.wMonitor == MONITOR_USER )
  448.         wTmp = IDS_FORMAT_USER;
  449.     else
  450.         wTmp = IDS_FORMAT_BOTH;
  451.  
  452.     // load the formatting string and build the label string
  453.     LoadString(GhInst, wTmp, szFormat, sizeof(szFormat) - 1);
  454.     wsprintf(GszBuf, szFormat, GwFree);
  455. } // FormatLabel
  456.  
  457.  
  458. // =================
  459. // end of resgauge.c
  460. // =================
  461.